<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <page-tools :show-before="false">
          <template slot="after">
            <el-button type="primary" plain @click="addPrem(1,'0')">新增权限</el-button>
          </template>
        </page-tools>
        <!-- <el-row type="flex" justify="end">
          <el-button size="small" type="success">添加权限</el-button>
        </el-row> -->
        <el-table :data="permList" border row-key="id" default-expand-all>
          <el-table-column align="center" label="权限名称" width="240" prop="name" />
          <el-table-column align="center" label="权限标记" width="240" prop="code" />
          <el-table-column align="center" label="权限描述" width="240" prop="description" />
          <el-table-column label="操作按钮">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="success" plain @click="addPrem(2,row.id)">添加权限</el-button>
              <el-button size="small" type="warning" plain @click="updatePrem(row.id)">编辑权限</el-button>
              <el-button size="small" type="danger" plain @click="deletePrem(row.id)">删除权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增弹窗 -->
      <el-dialog title="新增权限" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="addPrem" label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="激活状态">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button type="primary" @click="btnOK">确认</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </template></el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermList, addPrem, updatePerm, getPermDetail, delPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        pid: '',
        // nVisible 是否激活权限, 字符串 1 为激活, 字符串 0 为禁用
        enVisible: '1',
        // type 是指权限层级, 第一层为 1 第二层为 2
        type: 1
      }
    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    // 获取权限列表
    async getPermList() {
      // 扁平化数据转成树形结构
      this.permList = listToTreeData(await getPermList(), '0')
    },
    // 取消按钮
    btnCancel() {
      this.isShowDialog = false
    },
    // 新增
    addPrem(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async btnOK() {
      await this.$refs.addPrem.validate()
      if (this.formData.id) {
      // 编辑
        await updatePerm(this.formData)
      } else {
        // 新增
        await addPrem(this.formData)
      }
      this.$message.success('操作成功')
      this.isShowDialog = false
      this.getPermList()
    },
    // 编辑
    async  updatePrem(id) {
      this.formData = await getPermDetail(id)
      this.isShowDialog = true
    },
    // 删除
    async deletePrem(id) {
      try {
        await this.$confirm('是否确认删除?')
        await delPerm(id)
        this.$message.success('删除成功')
        this.getPermList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
